package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysDepartment;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SysDepartmentServiceImpl implements SysDepartmentService {

    @Autowired
    private SysDepartmentMapper departmentMapper;

    @Autowired
    private SysCompanyUserMapper companyUserMapper;

    @Override
    public List<SysDepartmentDTO> queryDepartmentList() {
        // 先查询所有一级部门
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId, 0);
        wrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);

        if (!CollectionUtils.isEmpty(departmentList)) {
            // 数据转换
            List<SysDepartmentDTO> departmentDTOList = BeanHelper.copyWithCollection(departmentList, SysDepartmentDTO.class);

            // 递归查询所有子部门
            queryChildrenDepartmentList(departmentDTOList);

            return departmentDTOList;

        }
        return null;
    }


    /**
     * 递归查询所有子部门
     *
     * @param departmentDTOList
     */
    private void queryChildrenDepartmentList(List<SysDepartmentDTO> departmentDTOList) {

        for (SysDepartmentDTO departmentDTO : departmentDTOList) {
            // 根据部门id，查询二级部门列表
            LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysDepartment::getParentId, departmentDTO.getId());
            List<SysDepartment> childrenList = departmentMapper.selectList(wrapper);

            if (!CollectionUtils.isEmpty(childrenList)) {
                List<SysDepartmentDTO> childrenDTOList = BeanHelper.copyWithCollection(childrenList, SysDepartmentDTO.class);
                // 查询三级部门
                queryChildrenDepartmentList(childrenDTOList);

                departmentDTO.setChildren(childrenDTOList);
            }

        }
    }


    @Override
    public List<Long> queryDepartmentIds() {
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);

        if (!CollectionUtils.isEmpty(departmentList)) {

            List<Long> idList = departmentList.stream().map(sysDepartment -> {
                return sysDepartment.getId();
            }).collect(Collectors.toList());

            return idList;
        }

        return null;
    }

    @Override
    public List<DepartmentOrUserSimpleDTO> querySimpleDepartment(Long id, Integer includeMember) {

        List<DepartmentOrUserSimpleDTO> dtoList = new ArrayList<>();

        // 查询指定部门id下的员工
        if (includeMember == 0) {
            LambdaQueryWrapper<SysCompanyUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysCompanyUser::getDepartmentId, id);
            wrapper.eq(SysCompanyUser::getCompanyId, CurrentUserHolder.get().getCompanyId());
            wrapper.eq(SysCompanyUser::getEnable, 1);
            List<SysCompanyUser> companyUserList = companyUserMapper.selectList(wrapper);

            if (!CollectionUtils.isEmpty(companyUserList)) {
                for (SysCompanyUser companyUser : companyUserList) {
                    DepartmentOrUserSimpleDTO simpleDTO = new DepartmentOrUserSimpleDTO();
                    simpleDTO.setId(companyUser.getId());
                    simpleDTO.setName(companyUser.getUserName());
                    simpleDTO.setType(2);
                    dtoList.add(simpleDTO);
                }
            }
        }

        // 查询指定部门下的子部门
        LambdaQueryWrapper<SysDepartment> deptWrapper = new LambdaQueryWrapper<>();
        deptWrapper.eq(SysDepartment::getParentId, id);
        deptWrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
        List<SysDepartment> departmentList = departmentMapper.selectList(deptWrapper);

        if (!CollectionUtils.isEmpty(departmentList)) {
            for (SysDepartment department : departmentList) {
                DepartmentOrUserSimpleDTO simpleDTO = new DepartmentOrUserSimpleDTO();
                simpleDTO.setId(department.getId());
                simpleDTO.setName(department.getName());
                simpleDTO.setType(1);
                dtoList.add(simpleDTO);
            }
        }

        return dtoList;
    }

    @Override
    public List<Long> queryDepartmentsByUserId(Long companyUserId) {

        // 存放所有的部门id，按照层级关系
        List<Long> deptIdList = new ArrayList<>();

        SysCompanyUser companyUser = companyUserMapper.selectById(companyUserId);
        if (companyUser != null) {
            // 递归依次查询上级部门
            getAllParentDeptIdWithSortById(companyUser.getDepartmentId(), deptIdList);
            return deptIdList;
        }

        return null;
    }

    /**
     * 递归依次查询上级部门
     *
     * @param departmentId
     * @param deptIdList
     */
    private void getAllParentDeptIdWithSortById(Long departmentId, List<Long> deptIdList) {

        deptIdList.add(departmentId);

        // 根据部门id查询部门信息
        SysDepartment department = departmentMapper.selectById(departmentId);

        if (department != null && department.getParentId()!=0) {
            getAllParentDeptIdWithSortById(department.getParentId(), deptIdList);
        }

    }


}
